﻿using System;
using System.IO;
using System.Web;
using VioletDevelopmentLaboratory.Net.Web.Configuration;

namespace VioletDevelopmentLaboratory.Net.Web.Component
{
	public sealed class PhysicalFileManager
	{
		private String saveFolderPath;
		public String SaveFolderPath { get { return this.saveFolderPath; } }

		private PhysicalFileManager()
		{
		}

		public PhysicalFileManager(String saveFolderPath)
		{
			this.saveFolderPath = saveFolderPath;
		}

		public static String PathCombine(String path1, params String[] paths)
		{
			String combinePath = path1;

			foreach (String path in paths)
			{
				combinePath = System.IO.Path.Combine(combinePath, path);
			}

			return combinePath;
		}

		public void Delete(String fileName)
		{
			if (fileName == null)
				throw new ArgumentNullException("fileName");

			if (String.IsNullOrEmpty(fileName))
				throw new ArgumentOutOfRangeException("fileName");

			File.Delete(Path.Combine(this.SaveFolderPath, fileName));
		}

		private String getUniqueFileName(String fileName)
		{
			return this.getUniqueFileName(this.SaveFolderPath, fileName);
		}

		private String getUniqueFileName(String folderPath, String fileName)
		{
			if (folderPath == null)
				throw new ArgumentNullException("folderPath");

			if (fileName == null)
				throw new ArgumentNullException("fileName");

			if (String.IsNullOrEmpty(folderPath))
				throw new ArgumentOutOfRangeException("folderPath");

			if (String.IsNullOrEmpty(fileName))
				throw new ArgumentOutOfRangeException("fileName");

			fileName = Path.GetFileName(fileName);
			Int32 _dotIndex = fileName.LastIndexOf('.');
			String _fileName = fileName.Substring(0, _dotIndex);
			String _fileExtension = fileName.Substring(_dotIndex + 1);
			Boolean _exists = true;
			Int32 _fileCount = 0;
			while (_exists)
			{
				if (File.Exists(Path.Combine(folderPath, fileName)))
				{
					_fileCount++;
					fileName = String.Concat(new object[] { _fileName, "[", _fileCount, "].", _fileExtension });
				}
				else
				{
					_exists = false;
				}
			}
			return fileName;
		}

		public void Save(HttpPostedFile postedFile, String uniqueFileName, out string saveFilePath)
		{
			if (postedFile == null)
				throw new ArgumentNullException("postedFile");

			if (uniqueFileName == null)
				throw new ArgumentNullException("uniqueFileName");

			if (String.IsNullOrEmpty(uniqueFileName))
				throw new ArgumentOutOfRangeException("uniqueFileName");

			DirectoryInfo saveFolder = new DirectoryInfo(this.SaveFolderPath);

			if (!saveFolder.Exists)
			{
				saveFolder.Create();
			}

			saveFilePath = "";
			String savePath = Path.Combine(this.SaveFolderPath, uniqueFileName);
			postedFile.SaveAs(savePath);
			saveFilePath = savePath;
		}

		public void Save(HttpPostedFile postedFile, out String saveFilePath)
		{
			if (postedFile == null)
				throw new ArgumentNullException("postedFile");

			DirectoryInfo saveFolder = new DirectoryInfo(this.SaveFolderPath);

			if (!saveFolder.Exists)
			{
				saveFolder.Create();
			}

			saveFilePath = "";
			String uniqueFileName = this.getUniqueFileName(postedFile.FileName);
			String savePath = Path.Combine(this.SaveFolderPath, uniqueFileName);
			postedFile.SaveAs(savePath);
			saveFilePath = savePath;
		}
	}
}
